package cn.b.sky.mes.pp.service.impl;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.b.sky.mes.andon.model.AndonConfig;
import cn.b.sky.mes.cm.model.ProcessComponent;
import cn.b.sky.mes.cm.model.ProcessOperation;
import cn.b.sky.mes.cm.model.WorkCenter;
import cn.b.sky.mes.cm.model.WorkUnit;
import cn.b.sky.mes.cm.service.ProcessService;
import cn.b.sky.mes.cm.service.WorkCenterService;
import cn.b.sky.mes.cm.service.WorkUnitService;
import cn.b.sky.mes.mm.enums.RequirementStatus;
import cn.b.sky.mes.mm.enums.TriggerStrategy;
import cn.b.sky.mes.mm.model.PFEP;
import cn.b.sky.mes.mm.model.PullStrategy;
import cn.b.sky.mes.mm.model.Requirement;
import cn.b.sky.mes.mm.model.Safetyinventroy;
import cn.b.sky.mes.mm.model.condition.SafetyinventroyCondition;
import cn.b.sky.mes.mm.service.InventoryService;
import cn.b.sky.mes.mm.service.PFEPService;
import cn.b.sky.mes.mm.service.PullStrategyService;
import cn.b.sky.mes.mm.service.RequirementService;
import cn.b.sky.mes.mm.service.SafetyinventroyService;
import cn.b.sky.common.exception.ImeException;
import cn.b.sky.user.SecurityContextUtil;
import cn.b.sky.mes.cm.enums.NumberRuleEnums;
import cn.b.sky.user.service.NumberRuleService;
import cn.b.sky.user.service.ParameterService;
import cn.b.sky.user.service.model.ParameterEntity;
import cn.b.sky.mes.pp.model.OperationTask;
import cn.b.sky.mes.pp.model.OperationTaskConfirm;
import cn.b.sky.mes.pp.model.ProductionConfirm;
import cn.b.sky.mes.pp.model.ProductionControl;
import cn.b.sky.mes.pp.model.ProductionOrder;
import cn.b.sky.mes.pp.service.OperationTaskService;
import cn.b.sky.mes.pp.service.ProductionControlService;
import cn.b.sky.mes.pp.service.RequirementStrategyService;

/**
 * 
 * <strong>Title : RequirementStrategyServiceImpl.java<br></strong>
 * <strong>Package : cn.b.sky.mes.mm.service.impl<br></strong>
 * <strong>Description : </strong>需求策略服务实现类<br> 
 * <strong>Create on : 2016年9月13日 上午9:51:53<br></strong>
 * <p>
 * @author dapeng.li<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 * <strong>修改历史:</strong><br>
 * 修改人|修改日期|修改描述<br>
 * -------------------------------------------<br>
 * <br>
 * <br>
 */
@Service("requirementStrategyService")
public class RequirementStrategyServiceImpl implements RequirementStrategyService {

  private static final Logger LOG = LoggerFactory.getLogger(RequirementStrategyServiceImpl.class);

  private static final String PULL_STRATEGY_ENABLE = "PULL_STRATEGY_ENABLE";

  @Autowired
  private ProcessService processService;

  @Autowired
  private WorkCenterService workCenterService;

  @Autowired
  private RequirementService requirementService;

  @Autowired
  private PullStrategyService pullStrategyService;

  @Autowired
  private PFEPService pFEPService;

  @Autowired
  private NumberRuleService numberRuleService;

  @Autowired
  private ProductionControlService productionControlService;

  @Autowired
  private WorkUnitService workUnitService;

  @Autowired
  private OperationTaskService operationTaskService;

  @Autowired
  private SafetyinventroyService safetyinventroyService;

  @Autowired
  private InventoryService inventoryService;

  @Autowired
  private ParameterService parameterService;

  /**
   * 
   * <strong>Title : batchSaveRequirements<br></strong>
   * <strong>Description : </strong>批量保存需求<br> 
   * <strong>Create on : 2016年9月18日 上午10:51:16<br></strong>
   * <p>
   * @param requirements
   * void
   * @throws 
   * @author dapeng.li<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  private void batchSaveRequirements(List<Requirement> requirements) {
    if (null != requirements && !requirements.isEmpty()) {
      requirementService.batchSaveRequirements(requirements);
    }
  }

  @Override
  public boolean isPullStrategyEnable() {
    try {
      ParameterEntity entity = parameterService.getParameterEntityByCode(PULL_STRATEGY_ENABLE,
          SecurityContextUtil.getSiteCode());
      return !StringUtils.isEmpty(entity.getDefaultValue())
          && Integer.valueOf(entity.getDefaultValue()).intValue() == 1;
    } catch (Exception e) {
      throw ImeException.newException("pullStrategyEnable.M", e);
    }
  }

  @Override
  public void createProductionOrderRequirements(ProductionOrder productionOrder,
                                                List<ProductionControl> productionControls) {
    // 根据订单查询工艺工序
    List<ProcessOperation> processOperations = processService.getProcessOperationByProcessCode(
        productionOrder.getSiteCode(), productionOrder.getProcessCode(), productionOrder.getProcessRev());
    // 根据工艺工序查询组件
    for (ProcessOperation processOperation : processOperations) {
      List<ProcessComponent> components = processService
          .getProcessComponentByProcessOperation(processOperation.getId());
      if (!components.isEmpty()) {
        processOperation.setComponent(components);
      }
    }
    // 生成需求列表
    batchSaveRequirements(createMaterialRequirements(productionControls, processOperations));
  }

  /**
   * 
   * <strong>Title : createMaterialRequirements<br></strong>
   * <strong>Description : </strong>创建物料需求<br> 
   * <strong>Create on : 2016年9月13日 下午1:24:10<br></strong>
   * <p>
   * @param productionControls
   * @param processOperations
   * void
   * @throws 
   * @author dapeng.li<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  private List<Requirement> createMaterialRequirements(List<ProductionControl> productionControls,
      List<ProcessOperation> processOperations) {
    List<Requirement> requirements = new LinkedList<Requirement>();
    for (ProductionControl productionControl : productionControls) {
      if (null == productionControl || null == productionControl.getPlannedQuantity()
          || productionControl.getPlannedQuantity().doubleValue() <= 0) {
        continue;
      }
      WorkCenter workCenter = workCenterService.getWorkCenterByPK(productionControl.getSiteCode(),
          productionControl.getWorkCenterCode());
      // 工作中心不存在无法生成需求
      if (null == workCenter) {
        continue;
      }
      for (ProcessOperation processOperation : processOperations) {
        List<Requirement> list = createMaterialRequirements(productionControl, workCenter, processOperation);
        if (!list.isEmpty()) {
          requirements.addAll(list);
        }
      }
    }
    return requirements;
  }

  /**
   * 
   * <strong>Title : createMaterialRequirements<br></strong>
   * <strong>Description : </strong>生成单个工序的需求<br> 
   * <strong>Create on : 2016年9月13日 下午1:33:53<br></strong>
   * <p>
   * @param control
   * @param workCenter 
   * @param processOperation
   * @return
   * Requirement
   * @throws 
   * @author dapeng.li<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  private List<Requirement> createMaterialRequirements(ProductionControl control, WorkCenter workCenter,
      ProcessOperation processOperation) {
    List<Requirement> requirements = new LinkedList<Requirement>();
    for (ProcessComponent component : processOperation.getComponent()) {
      PullStrategy pullStrategy = pullStrategyService.getPullStrategyByMaterial(control.getSiteCode(),
          component.getComponentCode());
      // 获取物料的拉动策略，如果需求触发策略为【生产订单定额】
      if (null != pullStrategy
          && pullStrategy.getRequirementTriggerModeCode().equals(TriggerStrategy.PRODUCTION_QUOTAS.getStrategy())) {
        Requirement requirement = new Requirement();
        requirement.setMaterialCode(component.getComponentCode());
        requirement.setProductionOrderNum(control.getProductionOrderNum());
        requirement.setProductionControlNum(control.getProductionControlNum());
        requirement.setProcessOperationId(processOperation.getId());
        requirement.setWorkcenterCode(workCenter.getWorkCenterCode());
        requirement.setRequirementQuantity(component.getQuantity() * control.getPlannedQuantity());
        // 设置策略相关属性及默认创建属性
        setRequirementProperties(requirement, pullStrategy);
        PFEP pfep = pFEPService.selectPFEP(control.getSiteCode(), component.getComponentCode());
        setWarehouseAndLocation(requirement, workCenter, pfep);
        requirement.setExpectedArrivalDateTime(
            new Date(requirement.getCreateDateTime().getTime() + (long) (pfep.getDistributionCycle() * 60000)));
        requirement.setDistributionModeCode(pfep.getDistributionModeCode());
        requirement.setDistributionUser(pfep.getDistributionUser());

        requirements.add(requirement);
      }
    }
    return requirements;
  }

  @Override
  public String insertManualRequirement(Requirement requirement) {
    PFEP pfep = pFEPService.selectPFEP(requirement.getSiteCode(), requirement.getMaterialCode());
    if (pfep == null) {
      throw ImeException.newException("noPFEP.M");
    }
    setCreateInfo(requirement);
    requirement.setDistributionModeCode(pfep.getDistributionModeCode());
    requirement.setDistributionUser(pfep.getDistributionUser());
    PullStrategy pullStrategy = pullStrategyService.getPullStrategyByMaterial(requirement.getSiteCode(),
        requirement.getMaterialCode());
    requirement.setPullStrategyCode(pullStrategy.getPullStrategyCode());
    requirement.setRequirementTriggerModeCode(TriggerStrategy.MANUAL.getStrategy());
    requirement.setDistributionTriggerModeCode(pullStrategy.getDistributionTriggerModeCode());
    requirement.setStatusECode(RequirementStatus.CREATED.getStatus());

    ProductionControl productionControl = productionControlService.selectProductionControl(requirement.getSiteCode(),
        requirement.getProductionControlNum());
    requirement.setProductionOrderNum(productionControl.getProductionOrderNum());

    boolean isFinded = false;
    //取仓库库位
    if (!StringUtils.isBlank(requirement.getWorkunitCode())) {
      WorkUnit workUnit = workUnitService.getWorkUnit(requirement.getWorkunitCode(), requirement.getSiteCode());
      if (!StringUtils.isBlank(workUnit.getWarehouseCode())) {
        isFinded = true;
        requirement.setWarehouseCode(workUnit.getWarehouseCode());
        requirement.setStorageLocationCode(workUnit.getStorageLocationCode());
      }
    }
    if (!isFinded) {
      WorkCenter workCenter = workCenterService.getWorkCenterByPK(productionControl.getSiteCode(),
          productionControl.getWorkCenterCode());
      if (!StringUtils.isBlank(workCenter.getWarehouseCode())) {
        isFinded = true;
        requirement.setWarehouseCode(workCenter.getWarehouseCode());
        requirement.setStorageLocationCode(workCenter.getStorageLocationCode());
      }
    }
    if (!isFinded) {
      requirement.setWarehouseCode(pfep.getWarehouseCode());
      requirement.setStorageLocationCode(pfep.getStorageLocationCode());
    }
    requirementService.insertRequirement(requirement);
    return requirement.getRequirementNum();
  }

  @Override
  public void createSafetyInventoryRequirements(ProductionConfirm confirm) {
    ProductionControl control = productionControlService.selectProductionControl(confirm.getSiteCode(),
        confirm.getProductionControlNum());
    List<ProcessOperation> operations = processService.getProcessOperationByProcessCode(confirm.getSiteCode(),
        control.getProcessCode(), control.getProcessRev());
    // 前一个确认点
    int prevConfirmIndex = -1;
    // 当前确认点
    int curConfirmIndex = 0;
    for (int i = 0; i < operations.size(); i++) {
      ProcessOperation operation = operations.get(i);
      if (operation.getId().intValue() == confirm.getProcessOperationId().intValue()) {
        curConfirmIndex = i;
        break;
      } else if (null != operation.getConfirmFlag() && operation.getConfirmFlag().intValue() == 1) {
        prevConfirmIndex = i;
      }
    }
    List<Requirement> requirements = new LinkedList<Requirement>();
    // 判断上一个确认点之后到当前确认点之间的安全库存
    for (int i = prevConfirmIndex + 1; i <= curConfirmIndex; i++) {
      ProcessOperation processOperation = operations.get(i);
      List<ProcessComponent> components = processService
          .getProcessComponentByProcessOperation(processOperation.getId());
      if (!components.isEmpty()) {
        processOperation.setComponent(components);
        List<Requirement> list = createSafetyInventoryRequirements(control, processOperation,
            confirm.getWorkUnitCode());
        if (!list.isEmpty()) {
          requirements.addAll(list);
        }
      }
    }
    batchSaveRequirements(requirements);
  }

  @Override
  public void createSafetyInventoryRequirements(OperationTaskConfirm confirm) {
    ProductionControl control = productionControlService.selectProductionControl(confirm.getSiteCode(),
        confirm.getProductionControlNum());
    OperationTask operationTask = operationTaskService.getOperationTask(confirm.getSiteCode(),
        confirm.getOperationTaskNum());
    ProcessOperation operation = processService.getProcessOperationById(operationTask.getProcessOperationId());
    List<ProcessComponent> components = processService.getProcessComponentByProcessOperation(operation.getId());
    if (!components.isEmpty()) {
      operation.setComponent(components);
      batchSaveRequirements(createSafetyInventoryRequirements(control, operation, confirm.getWorkUnitCode()));
    }
  }

  /**
   * 
   * <strong>Title : createSafetyInventoryRequirements<br></strong>
   * <strong>Description : </strong>创建单一工序的安全库存需求<br> 
   * <strong>Create on : 2016年9月18日 下午1:15:10<br></strong>
   * <p>
   * @param control
   * @param operation
   * @param workunitCode
   * @return
   * List<Requirement>
   * @throws 
   * @author dapeng.li<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  private List<Requirement> createSafetyInventoryRequirements(ProductionControl control, ProcessOperation operation,
      String workunitCode) {
    List<Requirement> requirements = new LinkedList<Requirement>();
    for (ProcessComponent component : operation.getComponent()) {
      PullStrategy pullStrategy = pullStrategyService.getPullStrategyByMaterial(control.getSiteCode(),
          component.getComponentCode());
      // 获取物料的拉动策略，如果需求触发策略为【安全库存】
      if (null != pullStrategy
          && pullStrategy.getRequirementTriggerModeCode().equals(TriggerStrategy.SAFETY_INVENTORY.getStrategy())) {
        Requirement requirement = new Requirement();
        requirement.setSiteCode(control.getSiteCode());
        setCreateInfo(requirement);
        requirement.setMaterialCode(component.getComponentCode());
        requirement.setProductionOrderNum(control.getProductionOrderNum());
        requirement.setProductionControlNum(control.getProductionControlNum());
        requirement.setProcessOperationId(operation.getId());

        setRequirementProperties(requirement, pullStrategy);

        PFEP pfep = pFEPService.selectPFEP(control.getSiteCode(), component.getComponentCode());

        requirement.setWorkcenterCode(control.getWorkCenterCode());
        requirement.setWorkunitCode(workunitCode);
        setWarehouseAndLocation(requirement, pfep);

        SafetyinventroyCondition condition = new SafetyinventroyCondition();
        condition.setSiteCode(requirement.getSiteCode());
        condition.setMaterialCode(requirement.getMaterialCode());
        condition.setWarehouseCode(requirement.getWarehouseCode());
        condition.setStorageLocationCode(requirement.getStorageLocationCode());
        Safetyinventroy safetyinventroy = safetyinventroyService.getSafetyInventory(condition);
        // 不存在安全库存
        if (null == safetyinventroy) {
          requirement = null;
          continue;
        }
        requirement.setRequirementQuantity(pfep.getDistributionQuantity());
        requirement.setExpectedArrivalDateTime(
            new Date(requirement.getCreateDateTime().getTime() + (long) (pfep.getDistributionCycle() * 60000)));
        requirement.setDistributionModeCode(pfep.getDistributionModeCode());
        requirement.setDistributionUser(pfep.getDistributionUser());

        double totalInventory = inventoryService.getInventory(requirement);
        double totalRequirementQuantity = requirementService.getHistoryRequirementCount(requirement);
        // 当（历史需求总和 + 当前库存 < 安全库存）时，产生需求
        if (totalInventory + totalRequirementQuantity < safetyinventroy.getSafetyInventory().doubleValue()) {
          requirements.add(requirement);
          // 需要产生的需求条数
          int totalCount = (int) Math
              .ceil((safetyinventroy.getSafetyInventory().doubleValue() - (totalInventory + totalRequirementQuantity))
                  / pfep.getDistributionQuantity());
          // 产生的需求+库存必须大于安全库存
          for (int i = 1; i < totalCount; i++) {
            try {
              Requirement item = (Requirement) requirement.clone();
              setCreateInfo(item);
              item.setExpectedArrivalDateTime(
                  new Date(requirement.getCreateDateTime().getTime() + (long) (pfep.getDistributionCycle() * 60000)));
              requirements.add(item);
            } catch (CloneNotSupportedException e) {
              LOG.error("{}", e);
            }
          }
        }
      }
    }
    return requirements;
  }

  /**
   * 
   * <strong>Title : setWarehouseAndLocation<br></strong>
   * <strong>Description : </strong>设置需求的线边库及库位（按照作业单元、工作中心、PFEP）<br> 
   * <strong>Create on : 2016年9月18日 上午11:12:19<br></strong>
   * <p>
   * @param requirement
   * @param pfep
   * void
   * @throws 
   * @author dapeng.li<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  private boolean setWarehouseAndLocation(Requirement requirement, PFEP pfep) {
    boolean isSet = false;
    // 优先以单元为准
    if (!StringUtils.isEmpty(requirement.getWorkunitCode())) {
      WorkUnit unit = workUnitService.getWorkUnit(requirement.getWorkunitCode(), pfep.getSiteCode());
      if (null != unit && !StringUtils.isEmpty(unit.getWarehouseCode())) {
        requirement.setWarehouseCode(unit.getWarehouseCode());
        requirement.setStorageLocationCode(unit.getStorageLocationCode());
        isSet = true;
      }
    }
    if (!isSet && !StringUtils.isEmpty(requirement.getWorkcenterCode())) {
      WorkCenter center = workCenterService.getWorkCenterByPK(pfep.getSiteCode(), requirement.getWorkcenterCode());
      isSet = setWarehouseAndLocation(requirement, center, pfep);
    }
    return isSet;
  }

  /**
   * 
   * <strong>Title : setWarehouseAndLocation<br></strong>
   * <strong>Description : </strong>设置需求的线边库及库位（按照工作中心、PFEP）<br> 
   * <strong>Create on : 2016年9月18日 上午11:38:26<br></strong>
   * <p>
   * @param requirement
   * @param center
   * @param pfep
   * @return
   * boolean
   * @throws 
   * @author dapeng.li<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  private boolean setWarehouseAndLocation(Requirement requirement, WorkCenter center, PFEP pfep) {
    boolean isSet = false;
    // 优先以工作中心为准
    if (!isSet && null != center && !StringUtils.isEmpty(center.getWarehouseCode())) {
      requirement.setWarehouseCode(center.getWarehouseCode());
      requirement.setStorageLocationCode(center.getStorageLocationCode());
      isSet = true;
    }
    // 其次以PFEP为准
    if (!isSet && null != pfep && !StringUtils.isEmpty(pfep.getWarehouseCode())) {
      requirement.setWarehouseCode(pfep.getWarehouseCode());
      requirement.setStorageLocationCode(pfep.getStorageLocationCode());
      isSet = true;
    }
    return isSet;
  }

  /**
   * 
   * <strong>Title : setRequirementProperties<br></strong>
   * <strong>Description : </strong>设置需求的策略属性及相关通用属性<br> 
   * <strong>Create on : 2016年9月18日 下午1:46:56<br></strong>
   * <p>
   * @param requirement
   * @param pullStrategy
   * void
   * @throws 
   * @author dapeng.li<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  private void setRequirementProperties(Requirement requirement, PullStrategy pullStrategy) {
    setCreateInfo(requirement);
    requirement.setPullStrategyCode(pullStrategy.getPullStrategyCode());
    requirement.setRequirementTriggerModeCode(pullStrategy.getRequirementTriggerModeCode());
    requirement.setDistributionTriggerModeCode(pullStrategy.getDistributionTriggerModeCode());
    requirement.setStatusECode(RequirementStatus.CREATED.getStatus());
  }

  /**
   * 
   * <strong>Title : setCreateInfo<br></strong>
   * <strong>Description : </strong>设置创建信息及需求编码<br> 
   * <strong>Create on : 2016年9月19日 上午9:31:25<br></strong>
   * <p>
   * @param requirement
   * void
   * @throws 
   * @author dapeng.li<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  private void setCreateInfo(Requirement requirement) {
    if(StringUtils.isBlank(requirement.getSiteCode())){
      requirement.setSiteCode(SecurityContextUtil.getSiteCode());
    }
    requirement.setRequirementNum(
        numberRuleService.selectNextNumber(requirement.getSiteCode(), NumberRuleEnums.REQUIREMENTNUM.getValue()));
    requirement.setCreateUser(SecurityContextUtil.getCurrentUserName());
    requirement.setCreateDateTime(new Date());
    requirement.setModifyUser(SecurityContextUtil.getCurrentUserName());
    requirement.setModifyDateTime(new Date());
  }

  @Override
  public void createAndonRequirement(AndonConfig andonConfig) {
    if (null != andonConfig.getTriggerMateriaLreqFlag() && andonConfig.getTriggerMateriaLreqFlag().intValue() == 1) {
      PullStrategy pullStrategy = pullStrategyService.getPullStrategyByMaterial(andonConfig.getSiteCode(),
          andonConfig.getMaterialCode());
      ProductionControl control = productionControlService.selectProductionControl(andonConfig.getSiteCode(),
          andonConfig.getProductionControlNum());
      // 获取物料的拉动策略，如果需求触发策略为【按灯】
      if (null != control && null != pullStrategy
          && pullStrategy.getRequirementTriggerModeCode().equals(TriggerStrategy.ANDON.getStrategy())) {
        Requirement requirement = new Requirement();
        requirement.setMaterialCode(andonConfig.getMaterialCode());
        requirement.setProductionOrderNum(control.getProductionOrderNum());
        requirement.setProductionControlNum(control.getProductionControlNum());
        requirement.setProcessOperationId(andonConfig.getProcessOperationId());

        setRequirementProperties(requirement, pullStrategy);

        PFEP pfep = pFEPService.selectPFEP(andonConfig.getSiteCode(), andonConfig.getMaterialCode());

        requirement.setWorkcenterCode(andonConfig.getWorkCenterCode());
        requirement.setWorkunitCode(andonConfig.getWorkUnitCode());
        setWarehouseAndLocation(requirement, pfep);
        requirement.setRequirementQuantity(andonConfig.getReqQuantity());
        requirement.setExpectedArrivalDateTime(
            new Date(requirement.getCreateDateTime().getTime() + (long) (pfep.getDistributionCycle() * 60000)));
        requirement.setDistributionModeCode(pfep.getDistributionModeCode());
        requirement.setDistributionUser(pfep.getDistributionUser());

        requirementService.insertRequirement(requirement);
      }
    }
  }

}
